//
//  WBNetworkingTools.swift
//  weibo
//
//  Created by yanghong on 15/12/7.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import  UIKit
import  AFNetworking
enum WBRequestType: String {
    case GET = "GET"
    case POST = "POST"
}
class WBNetworkingTools: AFHTTPSessionManager {
    
    // 定义一个闭包
    
    typealias WBNetworkingToolsCallBack = (response: AnyObject?, error: NSError?)->()
    
    // MARK - 创建一个网络工具的单例
    
    static let shareNetworking: WBNetworkingTools = {
        
        let netWorkingTools = WBNetworkingTools()
        
        netWorkingTools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return netWorkingTools
    }()
    //网络请求数据的方法     抽取之后的方法  --
    func request(Method: WBRequestType, URLString: String, parameters: AnyObject?, callBack: (response: AnyObject?, error: NSError?)->()) {
        
        let successBack = { (dataTask: NSURLSessionDataTask, response: AnyObject)->() in
        
        callBack(response: response, error: nil)
        
        }
        let failureBack = { (dataTask: NSURLSessionDataTask?, error: NSError)->() in
         
            callBack(response: nil, error: error)
        
        }
        
        if Method == .GET {
        
        self.GET(URLString, parameters: parameters, success: successBack, failure: failureBack)
        
        }
        
        if Method == .POST {
        
        self.POST(URLString, parameters: parameters, success: successBack, failure: failureBack)
        
        }
        
        
    }
    // MARK - 未抽取的代码
    func demo(Method: WBRequestType, URLString: String, parameters: AnyObject?, callBack: (response: AnyObject?, error: NSError?)->()) {
        if Method == .GET {
            
            self.GET(URLString, parameters: parameters, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                print(response)
                
                }, failure: { (_, error) -> Void in
                    
                    callBack(response: nil, error: error)
                    print(error)
            })
            
        }
        
        if Method == .POST {
            
            self.POST(URLString, parameters: parameters, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
                print(response)
                
                }, failure: { (_, error) -> Void in
                    
                    callBack(response: nil, error: error)
                    print(error)
            })
            
        }
    }

    
}
// MARK: - OAuth登录请求
extension WBNetworkingTools {
   
    func loadAccessToken(code: String, callBack: WBNetworkingToolsCallBack) {
    
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            
            "client_id": APPKEY,
            "client_secret": APP_SECRET,
            "grant_type": "authorization_code",
            "code":code,
            "redirect_uri": REDIRECT_URI
        ]
        request(.POST, URLString: urlString, parameters: params,callBack: callBack)

    
    }
    // 请求用户信息
    // https://api.weibo.com/2/users/show.json (获取用户信息接口)
    
   func loadUserInfo(uid: String, accessToken: String, callBack:WBNetworkingToolsCallBack) {
    
      let url = "https://api.weibo.com/2/users/show.json"
     let params = [
    
        "access_token": accessToken,
        "uid": uid
        
      ]
    //发送请求
    request(.GET, URLString: url, parameters: params, callBack: callBack)
 
    }

}
// MARK: - 首页微博数据请求
extension WBNetworkingTools {

    func loadHomeData(accessToken: String, maxid: Int64 = 0, sinceId: Int64 = 0, callback: WBNetworkingToolsCallBack) {
      //请求url
    let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
      // 请求参数
     let  params = [
        
        "access_token": accessToken ,
        "since_id": "\(sinceId)",
        "max_id": "\(maxid)"
        
        
        ]
      request(.GET, URLString: url, parameters: params, callBack: callback)
    }

}
